﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace Presentation
{
    public partial class CapNhatKhach : Form
    {
        public KhachHangDTO khach= new KhachHangDTO();
        public CapNhatKhach()
        {
            InitializeComponent();
        }
        public void layThongTinKhachDuocChon(KhachHangDTO khachHang)
        {
            khach = khachHang;
           // MessageBox.Show(khach.MaKhachHang.ToString(), "abc");

        }
        public void truyenThongTinVaoTextBox()
        {

            tb_MaKhachHang.Text = khach.MaKhachHang.ToString();
            tb_TenKhachHang.Text = khach.TenKhachHang;
            cbb_LoaiKhach.Text = khach.LoaiKhach.TenLoaiKhach;
            tb_CMND.Text = khach.CMND;
            tb_DiaChi.Text = khach.DiaChi;
        }
        private void CapNhatKhach_Load(object sender, EventArgs e)
        {
            layDSLoaiKhach();
            truyenThongTinVaoTextBox();
            
           // MessageBox.Show(khachEdit.TenKhachHang, "abc");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void layDSLoaiKhach()
        {
            List<LoaiKhachDTO> dsLoaiKhach = LoaiKhachBUS.layDSLoaiKhach();
            cbb_LoaiKhach.DataSource = dsLoaiKhach;
            cbb_LoaiKhach.DisplayMember = "TenLoaiKhach";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {

                khach.MaKhachHang = int.Parse(tb_MaKhachHang.Text.Trim());
                khach.TenKhachHang = tb_TenKhachHang.Text.Trim();
                khach.CMND = tb_CMND.Text.Trim();
                khach.DiaChi = tb_DiaChi.Text.Trim();

                LoaiKhachDTO loaiKhachDuocChon = (LoaiKhachDTO)cbb_LoaiKhach.Items[cbb_LoaiKhach.SelectedIndex];
                khach.LoaiKhach.MaLoaiKhach= loaiKhachDuocChon.MaLoaiKhach;


                bool ketQua = KhachHangBUS.capNhap(khach);
                if (ketQua == false)
                    throw new Exception();

                MessageBox.Show("Cập nhật thành công");
                //layDSPhong();

                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cập nhật thất bại");
            }

        }
        public KhachHangDTO layKhachMoiCapNhat()
        {
            return khach;
        }

    }
}
